using System;
using System.Collections.Generic;
using System.Text;
using DCI6.Client.API;
using DCI6;
using dds.tns;
using dds.ddms;

namespace DCIAPIRMSampleCode {
    internal class PublishingSampleCode {
        /**
         * Sample 13/14 schema XML of ItemData in this case its a Position Report
         * but any XML formatted data can be submitted.
         */
        //static String xmlSampleValidPosRpt1314 =
        //        "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
        //        "<PositionReport xmlns=\"http://peoc3t.us.army.mil/abcs/posRpt:2.0\">" +
        //        "<Security p2:classification=\"U\" p2:ownerProducer=\"ownerProducer1 ownerProducer2 ownerProducer3 \" p2:SCIcontrols=\"SCIcontrols1 SCIcontrols2 SCIcontrols3 \" p2:SARIdentifier=\"SARIdentifier1 SARIdentifier2 SARIdentifier3 \" p2:disseminationControls=\"disseminationControls1 disseminationControls2 disseminationControls3 \" p2:FGIsourceOpen=\"FGIsourceOpen1 FGIsourceOpen2 FGIsourceOpen3 \" p2:FGIsourceProtected=\"FGIsourceProtected1 FGIsourceProtected2 FGIsourceProtected3 \" p2:releasableTo=\"releasableTo1 releasableTo2 releasableTo3 \" p2:nonICmarkings=\"nonICmarkings1 nonICmarkings2 nonICmarkings3 \" p2:classifiedBy=\"classifiedBy1\" p2:derivativelyClassifiedBy=\"derivativelyClassifiedBy1\" p2:classificationReason=\"classificationReason1\" p2:derivedFrom=\"derivedFrom1\" p2:declassDate=\"1900-01-01\" p2:declassEvent=\"declassEvent1\" p2:declassException=\"declassException1 declassException2 declassException3 \" p2:typeOfExemptedSource=\"typeOfExemptedSource1 typeOfExemptedSource2 typeOfExemptedSource3 \" p2:dateOfExemptedSource=\"1900-01-01\" p2:declassManualReview=\"true\" xmlns:p2=\"urn:us:gov:ic:ism:v2\" xmlns=\"http://peoc3t.us.army.mil/abcs/common:2.0\" />" +
        //        "<Unit>" +
        //          "<ID xmlns=\"http://peoc3t.us.army.mil/abcs/common:2.0\">000001315010</ID>" +
        //          "<Name xmlns=\"http://peoc3t.us.army.mil/abcs/common:2.0\">MICAD10</Name>" +
        //          "<ParentName xmlns=\"http://peoc3t.us.army.mil/abcs/common:2.0\">SCT/HHC/1BN22IN</ParentName>" +
        //          "<UIC xmlns=\"http://peoc3t.us.army.mil/abcs/common:2.0\">ABC123</UIC>" +
        //          "<URN xmlns=\"http://peoc3t.us.army.mil/abcs/common:2.0\">1315010</URN>" +
        //          "<SymbolCode xmlns=\"http://peoc3t.us.army.mil/abcs/common:2.0\">SFGPEVU-----US-</SymbolCode>" +
        //          "<Service xmlns=\"http://peoc3t.us.army.mil/abcs/common:2.0\">ARM</Service>" +
        //        "</Unit>" +
        //        "<Location>" +
        //          "<Latitude xmlns=\"http://peoc3t.us.army.mil/abcs/common:2.0\">40.000000</Latitude>" +
        //          "<Longitude xmlns=\"http://peoc3t.us.army.mil/abcs/common:2.0\">-74.000000</Longitude>" +
        //          "<Altitude xmlns=\"http://peoc3t.us.army.mil/abcs/common:2.0\">1</Altitude>" +
        //          "<Precision xmlns=\"http://peoc3t.us.army.mil/abcs/common:2.0\">Precision1</Precision>" +
        //          "<ErrorEllipse xmlns=\"http://peoc3t.us.army.mil/abcs/common:2.0\">" +
        //            "<SemiMajor>SemiMajor1</SemiMajor>" +
        //            "<SemiMinor>SemiMinor1</SemiMinor>" +
        //            "<Orientation>Orientation1</Orientation>" +
        //          "</ErrorEllipse>" +
        //        "</Location>" +
        //        "<Course>0.0</Course>" +
        //        "<Speed>0.0</Speed>" +
        //        "<ReportDate>1900-01-01T01:01:01-05:00</ReportDate>" +
        //        "</PositionReport>";

        /**
         * Sample 11/12 schema XML of ItemData in this case its a Position Report
         * but any XML formatted data can be submitted.
         */

        private static String xmlSampleValidPosRpt1112 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                                                         "\n" +
                                                         "<!-- a comment -->\n" +
                                                         "\n" +
                                                         "<PositionReport xmlns=\"http://peoc3t.us.army.mil/abcs\">\n" +
                                                         "   <Unit>\n" +
                                                         "        <ID>ADAB00</ID>\n" +
                                                         "        <Name>ADAB00</Name>\n" +
                                                         "        <UIC>ADAB00</UIC>\n" +
                                                         "        <URN>1315010</URN>\n" +
                                                         "        <SymbolCode>SFGPUCD--------</SymbolCode>\n" +
                                                         "        <Service>ARM</Service>\n" +
                                                         "    </Unit>\n" +
                                                         "    <Latitude>5.1</Latitude>\n" +
                                                         "    <Longitude>5.2</Longitude>\n" +
                                                         "    <ReportDate>2005-04-19T00:06:37.000Z</ReportDate>\n" +
                                                         "    <Speed>0</Speed>\n" +
                                                         "</PositionReport>\n";

        public Publication CreatePublication(Advertisement advertisement) {
            /**
		     * There are multiple ways to publish to an Advertisement. You can
		     * provide from very little information, and then DCI will pre-populates
		     * most of it. Or you can provide as much detail as you want. We will
		     * demonstrate setting most of the Items Details Here.
		     */


            /**
		     * First you will need to Create an Item.
		     */
            Item item = new Item();
            /**
             * Assigns a String which should be well formatted XML to the
             * ItemData element of the Item. You can add multiple ItemData
             * XML per item however they must all have the same root Element
             * For example if you add a PositionReport xml they you can add
             * additional PositionReport elements but trying to add a Georef
             * will throw an exception.
             */
            item.add(xmlSampleValidPosRpt1112);

            /**
		     * Setting item ID (DCI will auto 
             * generate an ID based on timestamp) if an ID is not provided
		     */
            item.ItemID = "SampleCodeTestID";

            /**
		     * Setting classification
		     */
            item.Classification = Classification.UNCLASSIFIED;

            /**
		     * Setting AOI bounds. Corresponds to item/areaOfOccurance in
		     * dds.xsd
		     */
            item.DDSItem.AreaOfOccurrence = new dds.tns.AreaOfOccurrence();
            item.DDSItem.AreaOfOccurrence.WestBL = -1.0;
            item.DDSItem.AreaOfOccurrence.SouthBL = -1.0;
            item.DDSItem.AreaOfOccurrence.NorthBL = 1.0;
            item.DDSItem.AreaOfOccurrence.EastBL = 1.0;

            /**
		     * Setting start dtg and end dtg. Corresponds to
		     * item/itemTime/TimePeriod/start & end in dds.xsd
		     */
            DateTime startDTG = DateTime.Now;
            DateTime endDTG = DateTime.Now.AddYears(2);

            Period tempCoverage = new Period();
            tempCoverage.Start = startDTG;
            tempCoverage.End = endDTG;
            item.TimePeriod = tempCoverage;

            /**
		     * Setting keyword and category. Corresponds to
		     * item/subjectCoverage/Subject/category and
		     * item/subjectCoverage/Subject/keyword
		     */

            /**
		     * In order to add keywords and categories, need to set up 2
		     * holders: SubjectCoverageTypeType SubjectTypeType
		     */
            item.DDSItem.SubjectCoverage = new SubjectCoverage();
            item.DDSItem.SubjectCoverage.Subject = new Subject();
            item.DDSItem.SubjectCoverage.Subject.Subject_Choice = new Subject_Choice();

            /**
		     * Creating holder for category
		     */
            Category itemcat = new Category();
            /**
		     * Setting category
		     */
            itemcat.Label = "POS-RPT";

            /**
             * Creating holder for keyword
             */
            Keyword itemkw1 = new Keyword();
            /**
             * Setting keyword
             */
            itemkw1.Value = "MCS";

            /**
             * Creating second holder for keyword
             */
            Keyword itemkw2 = new Keyword();
            /**
             * Setting value for the second keyword
             */
            itemkw2.Value = "FBCB2";
            /**
             * According to dds.xsd: Categories must be set first then keywords.
             * The order is EXTREMELY IMPORTANT !!! Setting category and
             * keywords to the subjectType
             */
            item.DDSItem.SubjectCoverage.Subject.Subject_Choice.Category = itemcat;
            item.DDSItem.SubjectCoverage.Subject.Keyword.Add(itemkw1);
            item.DDSItem.SubjectCoverage.Subject.Keyword.Add(itemkw2);

            /**
		     * It is recommended that you use the publication object, 
             * based on the advertisement.
		     * Then add as many items to it and publish it. This example
             * will only show adding one item to the publication but you
             * should try to add more if you can.
		     */
            Publication pub = new Publication(advertisement);
            pub.add(item);

            return pub;
        }

        public void Publish(Publication publication, String ddsConfigConnectionID) {
            /**
             * Calling publish on the Publication Object will send the 
             * Publish Request to the DDS Configured with the connectionID
             * whose value is equal to the value stored in DDSConfigConnectionID.
             * If the DCI Service does not currently have the DDSConnectionID
             * registered an Exception will be thrown. If call is successfull then 
             * all Items added to the publication will be published to DDS.
             */
            Console.WriteLine("Sending Publish Request...");
            publication.publish(ddsConfigConnectionID);
            Console.WriteLine("Published [{0}] Items ", publication.Items.Count);
        }
    }
}